#include <check.h>
#include "test_version_info.h"

#include <signal.h>
#include "version_info.h"

static VersionInfo info; /* Compartida por los tests... */

static void teardown (void) {
	info = version_info_destroy (info);
}

/* Testeo de precondiciones */
START_TEST (test_new_null)
{
	info = version_info_new (NULL);
}
END_TEST

/* Testeo de funcionalidad */

START_TEST (test_no_file)
{
	info = version_info_new ("versioninfo/nofile");
	fail_unless (info.major == 0);
	fail_unless (info.minor == 0);
	fail_unless (info.subversion == 0);
	fail_unless (blength(info.tag) == 0);
}
END_TEST

START_TEST (test_no_tag)
{
	info = version_info_new ("versioninfo/notag");
	fail_unless (info.major == 2);
	fail_unless (info.minor == 6);
	fail_unless (info.subversion == 22);
	fail_unless (blength(info.tag) == 0);
}
END_TEST

START_TEST (test_no_dash_tag)
{
	info = version_info_new ("versioninfo/nodashtag");
	fail_unless (info.major == 2);
	fail_unless (info.minor == 6);
	fail_unless (info.subversion == 22);
	fail_unless (biseqcstr (info.tag, "generic"));
}
END_TEST

START_TEST (test_dash_tag)
{
	info = version_info_new ("versioninfo/dashtag");
	fail_unless (info.major == 2);
	fail_unless (info.minor == 6);
	fail_unless (info.subversion == 22);
	fail_unless (biseqcstr (info.tag, "-15-generic"));
}
END_TEST

START_TEST (test_destroy)
{
	info = version_info_new ("versioninfo/dashtag");
	info = version_info_destroy (info);
	fail_unless (info.tag == NULL);
}
END_TEST

/* Armado de la test suite */

Suite *version_info_suite (void)
{
	Suite *s = suite_create ("version_info");
	TCase *tc_preconditions = tcase_create ("Precondition");
	TCase *tc_functionality = tcase_create ("Functionality");

	/* Precondiciones */
	tcase_add_checked_fixture (tc_preconditions, NULL, teardown);
	tcase_add_test_raise_signal (tc_preconditions, test_new_null, SIGABRT);
	suite_add_tcase (s, tc_preconditions);

	/* Funcionalidad */
	tcase_add_checked_fixture (tc_functionality, NULL, teardown);
	tcase_add_test (tc_functionality, test_no_file);
	tcase_add_test (tc_functionality, test_no_tag);
	tcase_add_test (tc_functionality, test_no_dash_tag);
	tcase_add_test (tc_functionality, test_dash_tag);
	/* Destructor */
	tcase_add_test (tc_functionality, test_destroy);
	suite_add_tcase (s, tc_functionality);

	return s;
}

